package com.sdygmedia.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sdygmedia.domain.Category;
import com.sdygmedia.domain.CategoryChild;
import com.sdygmedia.service.CategoryChildService;
import com.sdygmedia.service.CategoryService;

@Controller
public class CategoryChildAction extends ActionSupport {

	private CategoryChild categoryChild;

	@Resource
	private CategoryChildService categoryChildService;
	@Resource
	private CategoryService categoryService;

	public String addUI() throws Exception {
		List categoryList = categoryService.findAll();
		ActionContext.getContext().put("categoryList", categoryList);
		return "addUI";
	}

	public String add() throws Exception {
		categoryChildService.save(getCategoryChild());
		//System.out.println(categoryChild.getCategory().getId());
		return "add";
	}

	public String delete() throws Exception {
		categoryChildService.delete(categoryChild.getId());
		return "toList";
	}

	public String updateUI() throws Exception {
		CategoryChild categoryChildUp = categoryChildService.getById(categoryChild.getId());
		ActionContext.getContext().put("categoryChildUp", categoryChildUp);
		List categoryList = categoryService.findAll();
		ActionContext.getContext().put("categoryList", categoryList);
		return "updateUI";
	}

	public String update() throws Exception {
		categoryChild = categoryChildService.getById(categoryChild.getId());
		categoryChildService.update(categoryChild);
		return "update";
	}

	public String list() throws Exception {
		List categoryChildList = categoryChildService.findAll();
		ActionContext.getContext().put("categoryChildList", categoryChildList);
		return "list";
	}

	// -----------------------------------------
	

	public CategoryChild getCategoryChild() {
		return categoryChild;
	}

	public void setCategoryChild(CategoryChild categoryChild) {
		this.categoryChild = categoryChild;
	}

	public CategoryChildService getCategoryChildService() {
		return categoryChildService;
	}

	public void setCategoryChildService(CategoryChildService categoryChildService) {
		this.categoryChildService = categoryChildService;
	}

}
